<%-- 
    Document   : GenerateSession
    Created on : Jun 27, 2011, 6:56:48 PM
    Author     : James
--%>

<%@ page contentType="text/xml" %>
<%@page import="org.joda.time.MutableDateTime, java.util.List, ejb.EntityState.TeacherState, ejb.EntityState.EmployeeFreeTimeState, ejb.Constants, java.util.ArrayList, org.joda.time.DateTime, org.joda.time.DateTimeZone" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<result>
<%
    ArrayList data   = (ArrayList) request.getAttribute("data");
    int numOfSession = ((Integer) data.get(0)).intValue() ;
    if (numOfSession == -1) {
%>
<status>failed</status>
<%
    } else {
        //Get the relevant information
        long start  = Long.parseLong(request.getParameter("start"));
        long today  = Long.parseLong(request.getParameter("today"));
        int day1    = Integer.parseInt(request.getParameter("day1"));
        int hour1   = Integer.parseInt(request.getParameter("hour1"));
        int hour2 = 0, hour3 = 0;

        int day2 = Integer.parseInt(request.getParameter("day2"));
        if (day2 != -1) hour2   = Integer.parseInt(request.getParameter("hour2"));
        
        int day3 = Integer.parseInt(request.getParameter("day3"));
        if (day3 != -1) hour3   = Integer.parseInt(request.getParameter("hour3"));

        //Render the Session time
        DateTimeZone studentZone = DateTimeZone.forID(request.getParameter("timezone"));
        DateTime thisDate = new DateTime(today, studentZone);
        DateTime endDate  = (new DateTime(thisDate.getYear(), thisDate.getMonthOfYear(), 1, 0, 0, 0, 0, studentZone)).plusMonths(4).minusSeconds(1);
        DateTime date     = new DateTime(start, studentZone);

        int         count = 0;
        boolean   matched = false;
        int     dayOfWeek = date.getDayOfWeek();
        MutableDateTime lesson = new MutableDateTime(studentZone);
        while (count < numOfSession && date.isBefore(endDate)) {
            if (dayOfWeek == day1) {
                matched = true;
                lesson.setDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), hour1, 0, 0, 0);
            } else if (dayOfWeek == day2) {
                matched = true;
                lesson.setDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), hour2, 0, 0, 0);
            } else if (dayOfWeek == day3) {
                matched = true;
                lesson.setDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), hour3, 0, 0, 0);
            }
            if (matched) {
                matched = false;
                count++;
%>
<session>
    <title>Session <%= count %></title>
    <start><%= lesson.getMillis()/1000 %></start>
</session>
<%
            }
            date      = date.plusDays(1);
            dayOfWeek = date.getDayOfWeek();
        }
        if (count == numOfSession) {
%>
<status>enoughSession</status>
<%
        } else {
%>
<status>notEnoughSession</status>
<%
        }
    }
%>          
</result>